package edu.cn.nlsde.tmfst.web.controller;

import edu.cn.nlsde.tmfst.model.User;
import edu.cn.nlsde.tmfst.web.common.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by dell on 2016/12/13.
 */

@Controller
@RequestMapping("/User")
public class UserController {
    //    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    public static final SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
    private static Logger mylog = LoggerFactory.getLogger(UserController.class.getName());

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "User/login";
    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseResult login(HttpSession httpSession, String user_name, String user_password) {
        ResponseResult result = new ResponseResult();
        User user = new User();
        user.user_name = user_name;
        user.user_password = user_password;
        if (!user.verify()) {
            result.status = false;
            result.msg = "登录失败";
        } else {
            result.status = true;
            httpSession.setAttribute("user", user.user_name);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    public ResponseResult reg(String user_name, String user_password) {
        ResponseResult result = new ResponseResult();
        User user = new User();
        user.user_name = user_name;
        user.user_password = user_password;
        if (!user.save_model()) {
            result.status = false;
            result.msg = "注册失败";
        } else {
            result.status = true;
        }
        return result;
    }
}
